package com.StarStudio.Controller;

import com.StarStudio.Domain.HttpStatusCode;
import com.StarStudio.Domain.MessageConstant;
import com.StarStudio.Domain.Result;
import com.StarStudio.Domain.Zone;
import com.StarStudio.Service.ZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/zones")
public class ZoneController {

    @Autowired
    private ZoneService zoneService;

    @GetMapping
    public Result getAll(){
        List<Zone> all = null;
        try {
            all = zoneService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_QUERY_SUCCESS, all);
    }

    @GetMapping("/{zoneId}")
    public Result getById(@PathVariable int zoneId){
        Zone byId = null;
        try {
            byId = zoneService.getById(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_QUERY_SUCCESS, byId);
    }

    @PostMapping
    public Result add(@RequestBody Zone zone) {
        try {
            zoneService.add(zone);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_ADD_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_ADD_SUCCESS);
    }

    @PutMapping
    public Result update(@RequestBody Zone zone){
        try {
            zoneService.update(zone);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_UPDATE_SUCCESS);
    }

    @DeleteMapping("{zoneId}")
    public Result delete(@PathVariable int zoneId){
        try {
            zoneService.delete(zoneId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.ZONE_DELETE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.ZONE_DELETE_SUCCESS);
    }

}
